Ext.namespace('Ext.expadminExpData');

var win;
var updflag;
Ext.onReady(function() {
	Ext.QuickTips.init();
	Ext.form.Field.prototype.msgTarget = 'side';

			win = addPbSocialTypeMasterInfo();

			// 主页面的城市下拉框
			var cityCombox = new Ext.form.ComboBox({
				fieldLabel : '城市',
				name : 'cityid',
				id:'cityid',
				store : getCityStore(),
			    displayField : 'CITY_NAME',
				valueField : 'CITY_ID',
				typeAhead : true,
				mode : 'remote',
				triggerAction : 'all',
				selectOnFocus : true,
				anchor : '90%',
     			forceSelection: true,
				emptyText : '请选择...',
				width : 150
			});
			// 主页面的省份的下拉框
			var proviceCombox = new Ext.form.ComboBox({
				fieldLabel : '省份',
				name : 'provinceid',
				id:'provinceid',
				store : getProvinceStore(),
				displayField : 'province_name',
				valueField : 'province_id',
				typeAhead : true,
				mode : 'remote',
				triggerAction : 'all',
				selectOnFocus : true,
				anchor : '90%',
     			forceSelection: true,
				emptyText : '请选择...',
				width : 150,
	            listeners:{
				    select:function(combo,record,index){
				    	cityCombox=Ext.getCmp('cityid')
				    	cityCombox.reset();
				    	cityStore=cityCombox.getStore();
						cityStore.proxy = new Ext.data.HttpProxy({
									url : 'City!queryCityByProviceId.action?province_id='
											+ proviceCombox.getValue()
								});
					
						cityStore.load();
				    }
				}
			});

			// 所属类别的下拉框
			var socialType = new Ext.form.ComboBox({
				fieldLabel : '所属类别',
				name : 'social_type',
				id:'social_type',
				store : getSocialType(),
				displayField : 'social_typename',
				typeAhead : true,
				valueField : 'social_typeid',
				mode : 'local',
				triggerAction : 'all',
				selectOnFocus : true,
				anchor : '90%',
   				forceSelection: true,
				emptyText : '请选择...',
				width : 150
			});

			var searchPanel = new Ext.form.FormPanel({
						frame : true,
						
						buttonAlign : 'right',
						title : "查询条件",
						id : 'employMessage',
						bodyStyle : 'padding:5px 5px 0',
						url : '',
						items : [{
									layout : 'column',
									items : [{
												columnWidth : .5,
												layout : 'form',
												items : [{
															layout : 'form',
															items : [proviceCombox]
														}, {
															fieldLabel : '社保项目名称',
															anchor : '90%',
															name : 'social_typename',
															id : 'social_typename',
															xtype : 'textfield'
														}]
											}, {
												columnWidth : .5,
												layout : 'form',
												items : [{
															layout : 'form',
															items : [cityCombox]
														}, {
															layout : 'form',
															items : [socialType]
														}]
											}],
									buttons : [{
												text : '新增',
												id : '_btninsert',
												handler : function() {
													updflag=false;
													Ext.getCmp('pbSocialTypeInfoPanel').getForm().reset();
													Ext.getCmp('socialMasterInfoGrid').getStore().removeAll();
													win.show();
												}
											}, {
												text : '查询',
												id : '_btnSearch',
												handler : function() {
													socialTypeMasterStore.on('beforeload', function() {
																socialTypeMasterStore.baseParams = {
																	'masFilter.provinceId' : Ext.getCmp('provinceid').getValue(),
																	'masFilter.cityId' : Ext.getCmp('cityid').getValue(),
																	'masFilter.socialTypeId' : Ext.getCmp('social_type').getValue(),
																	'masFilter.socialItemName' : Ext.getCmp('social_typename').getValue()
																}
															});
													socialTypeMasterStore.load();
												}
											}, {
												text : '删除',
												id : '_btnDelete',
												handler : function() {
										   		var length = pbSocialTypeMasterGrid.getSelectionModel().getSelections().length;
											   	if (length < 1) {
													return;
												}
											   	Ext.MessageBox.show({
													title : '提示',
													msg : '您选中了' + length + '条记录进行删除,请问您确认吗?',
													buttons : Ext.MessageBox.OKCANCEL,
													fn : function(btn) {
														if (btn == "ok") { 
															Ext.Ajax.request({
																url : "socialItem!deleteSocialMasterMutil.action?masterIds=" + pbSocialTypeMasterGrid.selModel.selections.keys,
																method : "post",
																success : function(response, options){
																	var responseTextVale = Ext.util.JSON.decode(response.responseText);
																	if(responseTextVale.hasSubCity>0){
																	   	Ext.Msg.alert('删除失败', '失败原因！');
																	}else{
																	socialTypeMasterStore.load();
																		Ext.Msg.alert('提示', '删除操作成功！');
																	}
																	
																}
															});
														}
														if (btn == "cancel") {
															return;
														}
													}
												}); 
												}
										}]
								}]
					});

			searchPanel.render(Ext.getBody());

			// 数据源
			socialTypeMasterStore = new Ext.data.Store({
						url : 'socialItem!queryPbSocialMasterResult.action',
						reader : new Ext.data.JsonReader({
									root : 'pbSocialMasterResult.result',
									id:'social_security_id',
									totalProperty : 'pbSocialMasterResult.totalCount'
								}, [{
											name : 'social_security_id',
											mapping : 'social_security_id'
										},{
											name : 'province_name',
											mapping : 'province_name'
										}, {
											name : 'city_name',
											mapping : 'city_name'
										}, {
											name : 'show_name',
											mapping : 'show_name'
										}, {
											name : 'pay_frequency',
											mapping : 'pay_frequency'
										}, {
											name : 'is_valid',
											mapping : 'is_valid'
										}, {
											name : 'social_typename',
											mapping : 'social_typename'
										}, {
											name : 'memo',
											mapping : 'memo'
										}])
					});

			// 查询显示的表格
			var sm = new Ext.grid.CheckboxSelectionModel();
			var cm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), sm,
					{
						header : 'ID',
						dataIndex : 'social_security_id',
						hidden:true
					},{
						header : '省份',
						width : 50,
						dataIndex : 'province_name'
					}, {
						header : '城市',
						dataIndex : 'city_name',
						width : 50
					}, {
						header : '社保项目名称',
						dataIndex : 'show_name',
						width : 140
					}, {
						header : '支付频率',
						dataIndex : 'pay_frequency',
						renderer : r_payFrequencyStore,
						width : 140
					}, {
						header : '是否有效',
						dataIndex : 'is_valid',
						renderer : r_isValid,
						width : 70
					}, {
						header : '所属类别',
						dataIndex : 'social_typename'
					}, {
						header : '说明',
						dataIndex : 'memo'
					}]);

			var pbSocialTypeMasterGrid = new Ext.grid.GridPanel({
						id : 'pbSocialTypeMaster',
						height : document.body.clientHeight
								- searchPanel.getInnerHeight()
								- searchPanel.getFrameHeight(),
						bbar : new Ext.PagingToolbar({
							pageSize : pageSize,
							store : socialTypeMasterStore,
							displayInfo : true,
							dispalyMsg : '显示第{0}条到{1}条记录，一共{2}条',
							emptyMsg : '没有记录'
						}),
						sm : sm,
						cm : cm,
						store : socialTypeMasterStore,
						loadMask : true,
						loadMask : {
							msg : '正在加载数据...'
						}
					});

			pbSocialTypeMasterGrid.render(Ext.getBody());

			socialTypeMasterStore.load();
			pbSocialTypeMasterGrid.addListener('rowdblclick', rowdblclickFn);

		});

function rowdblclickFn(grid, rowIndex, e) {
	updflag=true;
	Ext.Ajax.request({
		url:"socialItem!retrieveSocialSecurity.action?pbSocialMaster.social_security_id=" + grid.getStore().getAt(rowIndex).get('social_security_id'),
		method:"post",
		success: function(response,options){
				var responseTextValue = Ext.util.JSON.decode(response.responseText);
				Ext.getCmp('socialMasterInfoGrid').getStore().loadData(responseTextValue.pbSocialMaster.detailList,false);
				cityStore = cityCombox.getStore();
				cityStore.proxy = new Ext.data.HttpProxy({
							url : 'City!queryCityByProviceId.action?province_id=' + responseTextValue.pbSocialMaster.province_id
						});
				cityStore.load({callback:function() {
					Ext.getCmp('pbSocialTypeInfoPanel').getForm().setValues(responseTextValue.pbSocialMaster);
				}});
				
			}	
	});
	win.show();
}